﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;
using System.Linq;

namespace PET.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LiveController : ControllerBase
    {
            private readonly PetDbContext db;
            public LiveController(PetDbContext _context)
            {
                db = _context;

            }
            /// <summary>
            /// 查询数据
            /// </summary>
            /// <returns></returns>
            [HttpGet]
            public Result Get()
            {
                Result result = new Result();
                var resultData = db.Pet_live.ToList<Pet_live>();
                //result.Data = resultData;
                //result.Code = 200;
                //result.Name = "1598033581@qq.com";
                return result;
            }

        /// <summary>
        /// 用id查询数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        public Result Get(Guid Id)
        {
            Result result = new Result();
            Pet_live? resulrData = db.Pet_live.Where(w => w.ID == Id).FirstOrDefault<Pet_live>();
            //if (resulrData == null)
            //{
            //    result.Code = 404;
            //}
            //else
            //{
            //    result.Code = 200;
            //    result.Data = resulrData;
            //}
            return result;
        }
        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="live"></param>
        /// <returns></returns>

        [HttpPost]
            public Result Add([FromBody] Pet_live live)
            {
                Result result = new Result();
                db.Pet_live.Add(live);
                result.Data = db.SaveChanges();
                return result;
            }

            /// <summary>
            /// 删除数据
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            [HttpDelete]
            public Result Del(Guid id)
            {
                Result result = new Result();
            Pet_live? live = db.Pet_live.Where(w => w.ID == id).SingleOrDefault();
                if (live != null)
                    db.Pet_live.Remove(live);
                result.Data = db.SaveChanges();
                return result;
            }

            /// <summary>
            /// 修改数据
            /// </summary>
            /// <param name="live"></param>
            /// <returns></returns>
            [HttpPut]
            public Result Up([FromBody] Pet_live live)
            {
                Result result = new Result();
                db.Pet_live.Update(live);
                result.Data = db.SaveChanges();
                return result;
            }
        }
    }

